﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;

namespace System.Web.Mvc
{
    public static class MyHtmlExtents
    {
        public static string FixStrSuffix(this HtmlHelper helper, string s,int c) {
            if (string.IsNullOrEmpty(s)) return string.Empty;

            if (s.Length > c&&c>1)return s.Substring(0, c - 1) + "…";
            else return s;
        }
        public static string FixStrMiddle(this HtmlHelper helper, string s,int c) {
            if (string.IsNullOrEmpty(s)) return string.Empty;

            if (s.Length > c&&c>2)
            {
                return s.Substring(0, (int)c/2+1) + "…"+s.Substring(s.Length-(c-(int)c/2));
            }
            else return s;
        }
    }
    public static class MyLinqExtents {
        public static IQueryable<T> TryWhere<T>(this IQueryable<T> src, string val, Expression<Func<T, bool>> predicate) {
            if (!string.IsNullOrEmpty(val))
            {
                //if (typeof(V).IsValueType) { 
                //    Convert
                //}
                return src.Where(predicate);
            }
            else
            {
                return src;
            }
            
        }
    }
}